package com.dhcc.cdc.io;

import com.dhcc.core.framework.util.CommonUtil;

import java.lang.reflect.Method;
import java.util.ArrayList;

public class CdcDataService {
    public Object getData(String clazzName,String methodName,Object ... args) {
       
        try {
            Class<?> cls = Class.forName(clazzName); 
            Object obj = ApplicationContextHolder.getBean(cls);
            return invoke(obj, methodName, args);
        }catch(Exception e) {
            e.printStackTrace();
        }            
        return null;
    }

    public Object invoke(Object obj, String methodName, Object ... args) throws Exception{
        ArrayList<Class<?>> parameterTypes= new ArrayList<Class<?>>();
        int j=0;
        for(int i = 0; i < args.length; i++){
            if(CommonUtil.isEmpty(args[i])) continue;
            parameterTypes.add(args[i].getClass());
            j++;
        }
        Class<?>[] a_parameterTypes = new Class<?>[j];
        int k=0;
        Object[] a_args= new Object[j];
        for (Class<?> parameterType : parameterTypes) {
            a_parameterTypes[k]=parameterType;
            a_args[k]=args[k];
            k++;
        }
        Method method = obj.getClass().getDeclaredMethod(methodName, a_parameterTypes);
        return method.invoke(obj, a_args);
      }
}
